package cn.leon.service.impl;


import cn.leon.common.pojo.EasyUIDataGridResult;
import cn.leon.common.pojo.IDUtils;
import cn.leon.common.pojo.TaotaoResult;
import cn.leon.mapper.ItemMapper;
import cn.leon.mapper.Item_descMapper;
import cn.leon.pojo.Item;
import cn.leon.pojo.ItemExample;
import cn.leon.pojo.Item_desc;
import cn.leon.service.ItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private Item_descMapper item_descMapper;
    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        //分页处理
        PageHelper.startPage(page,rows);
        ItemExample itemExample = new ItemExample();
        //执行查询
        List<Item> itemList = itemMapper.selectByExample(itemExample);
        //封装到对象
        EasyUIDataGridResult easyUIDataGridResult = new EasyUIDataGridResult();
        easyUIDataGridResult.setRows(itemList);
        //取总记录数
        PageInfo<Item> pageInfo = new PageInfo<>(itemList);
        easyUIDataGridResult.setTotal(pageInfo.getTotal());
        return easyUIDataGridResult;
    }

    @Override
    public TaotaoResult addItem(Item item, String desc) {
        //生成商品Id
        long itemId = IDUtils.genItemId();
        //补全Item对象的属性
        item.setId(itemId);
        //商品状态，1-正常，2-下架，3-删除
        item.setStatus((byte) 1);
        Date date = new Date();
        item.setCreated(date);
        item.setUpdated(date);
        // 3、向商品表插入数据
        itemMapper.insert(item);
        // 4、创建一个ItemDesc对象
        Item_desc item_desc = new Item_desc();
        // 5、补全TbItemDesc的属性
        item_desc.setItemId(itemId);
        item_desc.setCreated(date);
        item_desc.setUpdated(date);
        item_desc.setItemDesc(desc);
        // 3、向商品描述表插入数据
        item_descMapper.insert(item_desc);
        return TaotaoResult.ok();
    }
}
